package com.sx.gongzhong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.LocationSelectAdapter;
import com.sx.gongzhong.model.LocationBean;
import com.sx.gongzhong.utils.AppConstants;
import com.sx.gongzhong.utils.BaseUtil;
import com.sx.gongzhong.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class LocationSelectActivity extends BaseActivity{

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.et_Search)
    EditText et_Search;
    @BindView(R.id.tv_Cancel)
    TextView tv_Cancel;
    private SuggestionSearch mSuggestionSearch;
    private List<SuggestionResult.SuggestionInfo> locationLists=new ArrayList<>();
    private LocationSelectAdapter locationAdapter;
    private String currentCity="北京";

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_location_select);
        setGoneTitle();
        currentCity=getIntent().getStringExtra("City");
        ButterKnife.bind(this);
        initViews();
    }

    private void initViews() {
        mSuggestionSearch = SuggestionSearch.newInstance();
        mSuggestionSearch.setOnGetSuggestionResultListener(listener);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(null);
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    swipeRefreshLayout.setRefreshing(false);
                }
            });
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        locationAdapter=new LocationSelectAdapter(this,R.layout.item_location_list,locationLists);
        locationAdapter.setEmptyView(View.inflate(this,R.layout.load_error_view,null));
        locationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                BaseUtil.hintKb(LocationSelectActivity.this);
                SuggestionResult.SuggestionInfo info=locationLists.get(position);
                LocationBean locationBean=new LocationBean(info.pt.latitude,info.pt.longitude,info.key);
                Intent intent=new Intent();
                intent.putExtra("Location",locationBean);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        recyclerView.setAdapter(locationAdapter);

        et_Search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()>0){
                    tv_Cancel.setText("搜索");
                }else {
                    tv_Cancel.setText("取消");
                }
            }
        });
        tv_Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("取消".equals(tv_Cancel.getText().toString())){
                    finish();
                }else {
                    // 使用建议搜索服务获取建议列表，结果在onSuggestionResult()中更新
                    String key=et_Search.getText().toString().trim();
                    if (!BaseUtil.isEmpty(key)){
                        LoadDialog.show(LocationSelectActivity.this,"加载中...");
                        BaseUtil.hintKb(LocationSelectActivity.this);
                        mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())
                                .keyword(key)
                                .city(currentCity));
                    }
                }
            }
        });
    }

    OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
        public void onGetSuggestionResult(SuggestionResult res) {
            LoadDialog.dismiss(LocationSelectActivity.this);
            if (res == null || res.getAllSuggestions() == null) {
                return;
                //未找到相关结果
            }
            locationLists.clear();
            locationLists.addAll(res.getAllSuggestions());
            locationAdapter.notifyDataSetChanged();

        }
    };

    @Override
    protected void onDestroy() {
        if (mSuggestionSearch!=null){
            mSuggestionSearch.destroy();
        }
        super.onDestroy();
    }

}
